﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;
using System.Globalization;

namespace ChamCong.Admin
{
    public partial class QuanLyTangCa : BasePage
    {
        ThamSoBLL tsBLL = new ThamSoBLL();
        LichTangCaBLL ltcBLL = new LichTangCaBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindDdlYear();
                ddlNam.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;

                bindGridView();

                string mathamso = "TTTangCa";
                rblTangCa.DataBind();

                for (int i = 0; i < rblTangCa.Items.Count; i++)
                {
                    if (int.Parse(rblTangCa.Items[i].Value) == int.Parse(tsBLL.LayGiaTri(mathamso)))
                    {
                        rblTangCa.Items[i].Selected = true;
                    }
                }

                AnTTTC();
            }
            //dgvLichTangCa.DataBind();
            //An();
            //dgvLichTangCa.DataBind();

            btnSua2.Visible = false;
        }

        protected void bindDdlYear()
        {
            for (int i = DateTime.Now.AddYears(1).Year; i >= 2010; i--)
            {
                ListItem obj = new ListItem();
                obj.Text = i.ToString();
                obj.Value = i.ToString();
                ddlNam.Items.Add(obj);
            }

        }

        protected void ddlNam_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindGridView();
        }

        protected void bindGridView()
        {
            if (ltcBLL.LayTatCaTheoNam(int.Parse(ddlNam.SelectedValue)).Count > 0)
            {
                dgvLichTangCa.DataSource = ltcBLL.LayTatCaTheoNam(int.Parse(ddlNam.SelectedValue));
                dgvLichTangCa.DataBind();
            }
            else
            {
                dgvLichTangCa.DataSource = ltcBLL.LayTatCaTheoNam(int.Parse(ddlNam.SelectedValue));
                dgvLichTangCa.DataBind();
                sbcMsgBox2.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Warning, "Thông báo", "Hiện thời không có dữ liệu!");
            }
        }

        protected void AnTTTC()
        {
            btnApDung.Visible = false;
            btnHuy.Visible = false;
            btnChinhSua.Visible = true;
            rblTangCa.Enabled = false;
        }


        protected void btnChinhSua_Click(object sender, EventArgs e)
        {
            btnChinhSua.Visible = false;
            btnHuy.Visible = true;
            btnApDung.Visible = true;
            rblTangCa.Enabled = true;
            UpdatePanel1.Update();
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            rblTangCa.DataBind();
            AnTTTC();
        }

        protected void btnApDung_Click(object sender, EventArgs e)
        {
            //rblTangCa.DataBind();             
            for (int i = 0; i < rblTangCa.Items.Count; i++)
            {
                if (rblTangCa.Items[i].Selected == true)
                {
                    THAMSO ts = new THAMSO();
                    string mathamso = "TTTangCa";
                    ts.MaThamSo = mathamso;
                    ts.GiaTri = rblTangCa.Items[i].Value;
                    tsBLL.SuaThamSo(ts);                    
                }
            }
            AnTTTC();
        }

        protected void btnThem2_Click(object sender, EventArgs e)
        {
            
            try
            {
                for (int i = 0; i < cblPhongban.Items.Count; i++)
                {
                    if (cblPhongban.Items[i].Selected == true)
                    {
                        IFormatProvider format = new CultureInfo("vi-VN");

                        LICHTANGCA ltc = new LICHTANGCA();
                        ltc.Ngay = (DateTime)Convert.ToDateTime(txtNgay.Text, format);
                        ltc.LyDo = txtLyDo.Text;
                        ltc.MaPB = int.Parse(cblPhongban.Items[i].Value);
                        ltcBLL.ThemLichTC(ltc);
                    }
                    
                }
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Đặt Lịch Thành Công!");
                Reset();
                bindGridView();
                
            }
            catch (Exception ex)
            {
                //lbthongbao.Text = ex.ToString();
            }
        }

        protected void dgvLichTangCa_rowdataboud(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgbtXoa = (ImageButton)e.Row.FindControl("imgbtXoa");
                ImageButton imgbtSua = (ImageButton)e.Row.FindControl("imgbtSua");
                ImageButton imgbtXem = (ImageButton)e.Row.FindControl("imgbtXem");

                if (imgbtXoa != null)
                    imgbtXoa.Attributes["Ngay"] = e.Row.Cells[0].Text;
                if (imgbtSua != null)
                    imgbtSua.Attributes["Ngay"] = e.Row.Cells[0].Text;
                if (imgbtXem != null)
                    imgbtXem.Attributes["Ngay"] = e.Row.Cells[0].Text;
            }
        }

        protected void imgbtXoa_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            IFormatProvider format = new CultureInfo("vi-VN");
            DateTime ngay = (DateTime)Convert.ToDateTime(btn.Attributes["Ngay"], format);
            //lbthongbao.Text = ngay.ToString();

            try
            {
                ltcBLL.Xoa(ngay);
                bindGridView();
            }
            catch
            {
                sbcMsgBox2.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Warning, "Thông báo", "Không thể xóa!");
            }
        }

        protected void An()
        {
            lbPhongBan.Visible = false;
            txtNgay.Visible = true;
            txtLyDo.Visible = true;
            cblPhongban.Visible = true;
            txtLyDo.Enabled = true;
            txtNgay.Enabled = true;
        }

        protected void Hien()
        {
            lbPhongBan.Visible = true;
            txtNgay.Visible = false;
            txtLyDo.Visible = false;
            cblPhongban.Visible = false;
        }

        protected void imgbtXem_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;

            IFormatProvider format = new CultureInfo("vi-VN");

            DateTime ngay = (DateTime)Convert.ToDateTime(btn.Attributes["Ngay"], format);
            
            txtNgay.Text = String.Format("{0:dd/MM/yyyy}", ngay);

            txtLyDo.Text = ltcBLL.LayLyDo(ngay).ToString();

            lbPhongBan.Visible = true;
            cblPhongban.Visible = false;
            txtNgay.Enabled = false;
            txtLyDo.Enabled = false;
            List<int> mapb = ltcBLL.LayPB(ngay);
            lbPhongBan.Text = "";
            foreach (var i in mapb)
            {
                PhongBanBLL pbBLL = new PhongBanBLL();
                lbPhongBan.Text = lbPhongBan.Text + "- " + pbBLL.LayPhongBan(i).TenPB.ToString() + "</br>";
            }
        }

        protected void imgbtSua_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;

            IFormatProvider format = new CultureInfo("vi-VN");
            DateTime ngay = (DateTime)Convert.ToDateTime(btn.Attributes["Ngay"], format);

            An();
            btnThem2.Visible = false;
            btnSua2.Visible = true;
           
            txtNgay.Text = String.Format("{0:dd/MM/yyyy}", ngay);
            txtNgay.Enabled = false;
            txtLyDo.Text = ltcBLL.LayLyDo(ngay).ToString();
            List<int> mapb = ltcBLL.LayPB(ngay);
            foreach (var i in mapb)
            {
                for (int j = 0; j < cblPhongban.Items.Count; j++)
                {
                    if (int.Parse(cblPhongban.Items[j].Value) == i)
                    {
                        cblPhongban.Items[j].Selected = true;
                    }
                }
            }
        }

        protected void btnSua2_Click(object sender, EventArgs e)
        {
            //LICHTANGCA ltc = new LICHTANGCA();
            //DateTime ngay = DateTime.ParseExact(txtNgay.Text, "dd/MM/yyyy", null);

            IFormatProvider format = new CultureInfo("vi-VN");
            DateTime ngay = (DateTime)Convert.ToDateTime(txtNgay.Text, format);
            
            ltcBLL.Xoa(ngay);

            try
            {
                for (int i = 0; i < cblPhongban.Items.Count; i++)
                {
                    if (cblPhongban.Items[i].Selected == true)
                    {
                        LICHTANGCA ltc = new LICHTANGCA();
                        ltc.Ngay = DateTime.ParseExact(txtNgay.Text, "dd/MM/yyyy", null);
                        ltc.LyDo = txtLyDo.Text;
                        ltc.MaPB = int.Parse(cblPhongban.Items[i].Value);
                        ltcBLL.ThemLichTC(ltc);
                    }

                }
                //lbthongbao.Text = "Sửa Lịch Tăng Ca Thành Công!";
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Sửa Lịch Tăng Ca Thành Công!");
                bindGridView();
                Reset();
            }
            catch
            {
                //lbthongbao.Text = "Sửa không được!";
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Warning, "Thông báo", "Sửa không được!");
            }
        }

        protected void btnHuy2_Click(object sender, EventArgs e)
        {
            Reset();
        }

        protected void Reset()
        {
            An();
            btnSua2.Visible = false;
            txtNgay.Text = "";
            txtNgay.Enabled = true;
            txtLyDo.Text = "";
            btnThem2.Visible = true;

            for (int i = 0; i < cblPhongban.Items.Count; i++)
            {
                cblPhongban.Items[i].Selected = false;
            }
        }
    }
}